package com.example.paimengshop.client_socket;
import org.json.simple.JSONObject;
import com.example.paimengshop.dataclass.*;

import java.io.*;
import java.util.ArrayList;

public class ActionControl_Send
{

    public ActionControl_Send()
    {

    }

    public static JSONObject Login(String email, String password)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("login and register", "login");
        jsonObject.put("email", email);
        jsonObject.put("password", password);
        return jsonObject;
    }


    public static JSONObject Register(String email, String password)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("login and register", "register");
        jsonObject.put("email", email);
        jsonObject.put("password", password);
        return jsonObject;
    }

    public static JSONObject SendVerCode_Register(String email, String password, int verCode)  //如果verCode输入错误，重新点击Register函数的按钮，不是SendVerCode函数的按钮
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("login and register", "register");
        jsonObject.put("email", email);
        jsonObject.put("password", password);
        jsonObject.put("verCode", verCode);
        return jsonObject;
    }

    public static JSONObject ChangeUserName(User user, String userName)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("change userInfo", "change userName");
        jsonObject.put("userId", Integer.toString(user.getId()));
        jsonObject.put("userName", userName);
        return jsonObject;
    }

    public static JSONObject ChangeAddress(User user, String address)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("change userInfo", "change address");
        jsonObject.put("userId", Integer.toString(user.getId()));
        jsonObject.put("address", address);
        return jsonObject;
    }

    public static JSONObject ChangePassword(User user, String email, String password)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("change userInfo", "change password");
        jsonObject.put("userId", Integer.toString(user.getId()));
        jsonObject.put("email", email);
        jsonObject.put("password", password);
        return jsonObject;
    }

    public static JSONObject ChangeHeadImageURL(User user, String headImageURL)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("change userInfo", "change headImageURL");
        jsonObject.put("userId", Integer.toString(user.getId()));
        jsonObject.put("headImageURL", "file:" + headImageURL);
        return jsonObject;
    }

    public static JSONObject SendVerCode_ChangePassword(String email, String password, int verCode)  //如果verCode输入错误，重新点击Register函数的按钮，不是SendVerCode函数的按钮
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("change userInfo", "change password");
        jsonObject.put("email", email);
        jsonObject.put("new password", password);
        jsonObject.put("verCode", verCode);
        return jsonObject;
    }

    public static JSONObject UpdateCart(User user, ArrayList<Commodity> commodityArrayList)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("update cart and collection", "update cart");
        jsonObject.put("userId", Integer.toString(user.getId()));
        jsonObject.put("commodityArrayList", Commodity.commodityArrayListToString(commodityArrayList));
        return jsonObject;
    }

    public static JSONObject UpdateCollection(User user, ArrayList<Commodity> commodityArrayList)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("update cart and collection", "update collection");
        jsonObject.put("userId", Integer.toString(user.getId()));
        jsonObject.put("commodityArrayList", Commodity.commodityArrayListToString(commodityArrayList));
        return jsonObject;
    }

    public static JSONObject CreateOrder(User user, Order order)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("create order", "");
        jsonObject.put("userId", order.getUserId());
        jsonObject.put("address", user.getAddress());
        String commodityArrayList = "";
        for(int i = 0; i < order.getCommodityList().size(); i++)
        {
            commodityArrayList += Integer.toString(order.getCommodityList().get(i).getId());
            commodityArrayList += ",";
        }
        jsonObject.put("commodityArrayList", commodityArrayList);
        jsonObject.put("commodity number", Integer.toString(order.getCommodityNumber()));
        jsonObject.put("payment", Double.toString(order.getPayment()));
        return jsonObject;
    }

    public static JSONObject AskForRefreshingUserInfo(int userId)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ask", "ask for refreshing userInfo");
        jsonObject.put("userId", Integer.toString(userId));
        return jsonObject;
    }

    public static JSONObject AskForRefreshingUserInfoByEmail(String email)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ask", "ask for refreshing userInfo");
        jsonObject.put("email", email);
        return jsonObject;
    }

    public static JSONObject AskForRefreshingCommodityInfo()
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ask", "ask for refreshing commodityInfo");
        return jsonObject;
    }

    public static JSONObject AskForRefreshingOrderInfo(int userId)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ask", "ask for refreshing orderInfo");
        jsonObject.put("userId", Integer.toString(userId));
        return jsonObject;
    }

    public static JSONObject Communication(String message, int userId)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("communication", "");
        jsonObject.put("userId", Integer.toString(userId));
        jsonObject.put("message", message);
        return jsonObject;
    }

    public static JSONObject Communication(File file, int userId)
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("communication", "");
        jsonObject.put("userId", Integer.toString(userId));
        byte[] dest = null;
        // 2、选择流
        InputStream is = null;
        // 有新增方法不能发生多态
        ByteArrayOutputStream baos = null;
        try
        {
            is = new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            // 3、操作(分段读取)
            byte[] flush = new byte[1024 * 10];// 缓冲容器
            int len = -1;// 接收长度
            try
            {
                while ((len = is.read(flush)) != -1)
                {
                    // 写出到字节数组中
                    baos.write(flush, 0, len);
                }
                baos.flush();
                // 返回回来，上面调用时就有了
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } finally
        {
            // 4、释放资源
            try
            {
                if (null != is)
                {
                    is.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        jsonObject.put("image", baos.toByteArray());
        return jsonObject;
    }
}
